package com.joelemon.mockinfluxdbdata.mock.service;

import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Description: 模拟数据接口工厂
 * @Author: HJY
 * @Date: 2023/11/20
 */
@Service
@Slf4j
public class MockHandleServiceFactory<T> {

    private Map<String, MockHandleService> mockHandleServiceMap;

    @Autowired
    private List<MockHandleService> mockHandleServices;

    @PostConstruct
    public void init() {
        if (mockHandleServices != null && mockHandleServices.size() > 0) {
            mockHandleServiceMap = mockHandleServices.stream()
                    .filter(service -> {
                        String handleClass = service.handleClass().getName();
                        return handleClass != null && handleClass != "";
                    }).collect(Collectors.toMap(item -> item.handleClass().getName(), Function.identity()));
        } else {
            mockHandleServiceMap = new HashMap<>();
        }
    }

    public MockHandleService<T> getMockHandleService(T mockConfig) {
        return mockHandleServiceMap.get(mockConfig.getClass().getName());
    }


    public MockHandleService<T> getMockHandleService(String configName) {
        return mockHandleServiceMap.get(configName);
    }

}
